package com.jxtti.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.jxtti.entity.SiteMenu;
import com.jxtti.service.SiteMenuService;
import com.jxtti.utils.AuthUtil;
import com.jxtti.utils.ExceptionUtil;



@Controller
@RequestMapping("/SiteMenu") 
public class SiteMenuController {
	private final static Logger log= Logger.getLogger(SiteMenuController.class);
	
	@Autowired //自动注入，不需要生成set方法了，required=false表示没有实现类，也不会报错。
	private SiteMenuService  siteMenuService; 
	
	@InitBinder  //初始化数据的时候，进行数据类型转换，String类型转为Date类型
    public void initBinder(WebDataBinder binder) {     
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");     
        dateFormat.setLenient(false);     
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));//true:���������ֵ��false:����Ϊ��ֵ 
    }
	
	
	/**
	 * 通过父节点ID获取菜单列表
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/findByPid", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> findByPid(
			@RequestParam Map<String, Object> data, HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> result = new HashMap<String, Object>();
		try {			
			data.put("issue", "1");
			System.out.println(AuthUtil.getUserInfo(request).getRoleId());
			if(AuthUtil.getUserInfo(request).getRoleId()== null){ //还未添加网站角色
				result.put("success", false);
				result.put("error", "抱歉，你没有权限，请联系网站管理员！");
			}else{
				data.put("roleId", AuthUtil.getUserInfo(request).getRoleId()); //获取角色id
				List<SiteMenu>  menuList= siteMenuService.findByPid(data);			
				result.put("menuList", menuList);
				result.put("success", true);
			}			
		} catch (Exception e) {
			result.put("success", false);
			result.put("error", ExceptionUtil.createStackTrackMessage(e));			
			e.printStackTrace();
		}
		return result;
	}

	
	@RequestMapping(value = "/listInterface")
	@ResponseBody
	public ModelAndView listInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/Menu/list.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
	
	
	/**
	 * 返回easyui 树结构
	 * , method = RequestMethod.POST
	 * @param data
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getEasyUiTree")
	@ResponseBody
	public List<Map<String, Object>> getEasyUiTree(
			@RequestParam Map<String, String> data, HttpServletRequest request) {
		List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
		try {
			result = siteMenuService.getEasyUiTree(data);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}
	
	/**
	 * 查询所有数据,
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/querySiteMenuAll")
	@ResponseBody
	public List<SiteMenu> queryTCityAQIWriteAll(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) throws Exception{
		
		data.put("is_deleted", "0");		
		List<SiteMenu>  SiteMenu= siteMenuService.queryAll(data);
		return SiteMenu;
	}
	
	/**
	 * 点击添加跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/addInterface")
	@ResponseBody
	public ModelAndView addInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/Menu/edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}	
	
	/**
	 * 添加或修改数据
	 * @param url
	 * @param classifyId
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/save")
	@ResponseBody	
	public Map<String,Object> save(@RequestParam Map<String, Object> data,
			SiteMenu  SiteMenu, HttpServletRequest request,	HttpServletResponse response) throws Exception{		
		ModelAndView mav = new ModelAndView();
		try {				
			
			int exeCount = siteMenuService.add(SiteMenu);
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}	
	
	
	/**
	 * 点击修改跳转到增加界面
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateInterface")
	@ResponseBody
	public ModelAndView updateInterface(@RequestParam Map<String, Object> data,
			HttpServletRequest request,SiteMenu  SiteMenu,
			HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		try {
			SiteMenu = siteMenuService.queryById(SiteMenu);		
			data.put("siteMenu", SiteMenu);
			mav.addObject("data", data);
			mav.setViewName("/pages/Admin/Site/Menu/edit.jsp");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return mav;
	}
		
	/**
	 * 修改信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> update(@RequestParam Map<String, Object> data,
			SiteMenu  SiteMenu, HttpServletRequest request,
			HttpServletResponse response) {
		try {			
			int exeCount;
			if(data.get("type")!=null&&data.get("type").equals("upDown")){ //上移下移
				SiteMenu tSiteMenu=siteMenuService.queryById(SiteMenu);
				tSiteMenu.setSortID(SiteMenu.getSortID());
				exeCount = siteMenuService.update(tSiteMenu);
			}else{
				exeCount = siteMenuService.update(SiteMenu);
			}
			
			if (exeCount > 0) {
				data.put("success", true);
			} else {
				data.put("success", false);
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
	
	/**
	 * 删除信息
	 * 
	 * @param data
	 * @param request
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/delete")
	@ResponseBody
	public Map<String, Object> delete(@RequestParam Map<String, Object> data,
			HttpServletRequest request,	HttpServletResponse response) {
		try {			
			String[] idArr=data.get("ids").toString().split(",");
			for(String id:idArr){
				SiteMenu  siteMenu=new SiteMenu();
				siteMenu.setId(Integer.parseInt(id));											
				int exeCount=siteMenuService.delete(siteMenu);
				if (exeCount > 0) {
					data.put("success", true);
				} else {
					data.put("success", false);
				}
			}
		} catch (Exception e) {
			data.put("success", false);
			data.put("error", ExceptionUtil.createStackTrackMessage(e));
			e.printStackTrace();
		}
		return data;
	}
	
}
